﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace ZGameFramework.Resource
{
    internal sealed class ResourceManager : GameFrameworkModule,IResourceManager
    {
        IResourceLoader m_ResourceLoader;

        public ResourceManager()
        {
            switch (ResourceConst.resourceLoaderType)
            {
                case ResourceLoaderType.e_Assetbundle:
                    SetResourceLoader( new AssetbundleLoader() );
                    break;
                case ResourceLoaderType.e_Resources:
                    SetResourceLoader( new ResourcesLoader() );
                    break;
            }
        }

        /// <summary>
        /// 获取游戏框架模块优先级。
        /// </summary>
        /// <remarks>优先级较高的模块会优先轮询，并且关闭操作会后进行。</remarks>
        internal override int Priority
        {
            get
            {
                return 0;
            }
        }

        /// <summary>
        /// 游戏框架模块轮询。
        /// </summary>
        /// <param name="elapseSeconds">逻辑流逝时间，以秒为单位。</param>
        /// <param name="realElapseSeconds">真实流逝时间，以秒为单位。</param>
        internal override void Update(float elapseSeconds, float realElapseSeconds)
        { }

        /// <summary>
        /// 关闭并清理游戏框架模块。
        /// </summary>
        internal override void Shutdown()
        {
            UnloadAssetAll();
        }


        public void SetResourceLoader(IResourceLoader resourceLoader)
        {
            if (null == resourceLoader)
            {
                //todo 抛出异常
                return;
            }

            m_ResourceLoader = resourceLoader;
        }


        public void LoadAssetAsync<T>( string assetPath) where T: Object
        {
            if (null == m_ResourceLoader)
            {
                return;
            }

            m_ResourceLoader.LoadAssetAsync<T>(assetPath);
        }

        public T LoadAssetSync<T>(string assetPath) where T : Object
        {
            if (null == m_ResourceLoader)
            {
                return default(T);
            }

            return m_ResourceLoader.LoadAssetSync<T>(assetPath);
        }

        public void UnloadAsset<T>(T t) where T : Object
        {
            if (null == m_ResourceLoader)
            {
                return;
            }

            m_ResourceLoader.UnloadAsset<T>(t);
        }
    
        /// <summary>
        /// 释放全部资源
        /// </summary>
        public void UnloadAssetAll()
        {
            if (null == m_ResourceLoader)
            {
                return;
            }
            m_ResourceLoader.UnloadAssetAll();
        }
    }
}
